package com.obtk.controllerSW;

//import cn.hutool.core.lang.UUID;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.obtk.model.CstService;
import com.obtk.service.CstServiceService;
import com.obtk.util.CoreConst;
import com.obtk.util.PageUtil;
import com.obtk.util.ResultUtil;
import com.obtk.vo.base.PageResultVo;
import com.obtk.vo.base.ResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

/**
 * 功能说明
 *
 * @author Mr.Sun
 * @date 2023-04-04 10:30:21
 */
@Controller
@RequestMapping("/cstService")
public class CstServiceController {
    @Autowired
    private CstServiceService cstServiceService;
    @PostMapping("/list")
    @ResponseBody
    public PageResultVo loadUsers(CstService cstService, Integer limit, Integer offset, Model model){
        PageHelper.startPage(PageUtil.getPageNo(limit, offset),limit);
        List<CstService> cstServicesList = cstServiceService.selectCstService(cstService);
        PageInfo<CstService> pages = new PageInfo<>(cstServicesList);
        System.out.println(cstServicesList);

        return ResultUtil.table(cstServicesList,pages.getTotal());
    }
    /**新增用户*/
    @PostMapping("/add")
    @ResponseBody
    public ResponseVo add(CstService cstServiceForm){
        Integer plaChcId = cstServiceForm.getPlaChcId();
        CstService cstService = cstServiceService.selectByPlaChCid(plaChcId);
        if (null != cstService) {
            return ResultUtil.error("订单已存在");
        }

        int num = cstServiceService.register(cstServiceForm);
        if(num > 0){
            return ResultUtil.success("添加用户成功");
        }else {
            return ResultUtil.error("添加用户失败");
        }
    }


    /**编辑用户详情*/
    @GetMapping("/edit")
    public String userDetail(Model model, Integer plaId){
        CstService cstService = cstServiceService.selectByCstServiceId(plaId);
        System.out.println("controller"+plaId);
        model.addAttribute("cstService", cstService);
        return "cstService/update";
    }

    /**编辑用户*/
    @PostMapping("/edit")
    @ResponseBody
    public ResponseVo editUser(CstService cstServiceForm){

        int a = cstServiceService.updateByCstServiceId(cstServiceForm);
        if (a > 0) {
            return ResultUtil.success("编辑开发计划成功！");
        } else {
            return ResultUtil.error("编辑开发计划失败");
        }
    }
    /**删除商品*/
    @GetMapping("/delete")
    @ResponseBody
    public ResponseVo deleteUser(String plaId) {
        List<String> cstServiceIdsList = Arrays.asList(plaId);
        int a = cstServiceService.updateStatusBatch(cstServiceIdsList, CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除开发计划成功");
        } else {
            return ResultUtil.error("删除开发计划失败");
        }
    }
    @GetMapping("/batch/delete")
    @ResponseBody
    public ResponseVo batchDeleteUser(String plaIdStr) {
        String[] plaIds = plaIdStr.split(",");
        List<String> cstServiceIdsList = Arrays.asList(plaIds);
        int a = cstServiceService.updateStatusBatch(cstServiceIdsList,CoreConst.STATUS_INVALID);
        if (a > 0) {
            return ResultUtil.success("删除开发计划成功");
        } else {
            return ResultUtil.error("删除开发计划失败");
        }
    }
}
